package com.atguigu.auth.activiti;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @autor 雷记新
 */
@SpringBootTest
public class ProcessTestDemo2 {
    //可以得到流程定义信息
    //注入RepositoryService
    @Autowired
    private RepositoryService repositoryService;

    //注入RuntimeService
    //流程实列
    @Autowired
    private RuntimeService runtimeService;

    //注入TaskService
    //流程任务
    @Autowired
    private TaskService taskService;

    //注入
    //历史相关操作
    @Autowired
    private HistoryService historyService;

    // 流程部署
    @Test
    public void deployProcess02() {
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/jiaban02.bpmn20.xml")
                .name("加班申请流程02")
                .deploy();
        System.out.println(deploy.getId());
        System.out.println(deploy.getName());
    }

    //启动流程实列
    @Test
    public void startProcessInstance(){
        Map<String,Object> map = new HashMap<>();
        //设置任务人
        map.put("assignee1","lucy02");
       // map.put("assignee2","mary02");
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("jiaban", map);
        System.out.println(processInstance.getProcessDefinitionId());
        System.out.println(processInstance.getId());
    }

    //查询个人的代办任务--zhangsan
    @Test
    public void findTaskLIst(){
        String aggign = "lucy02";
        List<Task> list = taskService.createTaskQuery()
                .taskAssignee(aggign).list();
        for (Task task : list){
            System.out.println("流程实例id：" + task.getProcessInstanceId());
            System.out.println("任务id：" + task.getId());
            System.out.println("任务负责人：" + task.getAssignee());
            System.out.println("任务名称：" + task.getName());
        }

    }


    //查询已经处理的任务
    @Test
    public void findcompleteTasklist(){
        List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery()
                .taskAssignee("lisi").finished().list();
        for (HistoricTaskInstance historicTaskInstance:list){
            System.out.println("流程实例id：" + historicTaskInstance.getProcessInstanceId());
            System.out.println("任务id：" + historicTaskInstance.getId());
            System.out.println("任务负责人：" + historicTaskInstance.getAssignee());
            System.out.println("任务名称：" + historicTaskInstance.getName());
        }
    }

    //处理当前任务
    @Test
    public void completeTask(){
        //查询负责人处理的任务，返回一条
        Task task = taskService.createTaskQuery()
                .taskAssignee("jack")
                .singleResult();
        //完成任务,参数是任务id
        taskService.complete(task.getId());

    }


    //启动流程实列
    @Test
    public void startProcess(){
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("请假");
        System.out.println("流程ID"+processInstance.getProcessDefinitionId());
        System.out.println("流程实列ID"+processInstance.getId());
        System.out.println("流程活动id"+processInstance.getActivityId());
    }

    //启动流程实列，添加businesskey
    @Test
    public void startupProcessAddBusinessKey(){
        String businessKeyy = "1";
        //启动流程实列，指定业务标识businesskey，也就是请假申请单id
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("请假", businessKeyy);
        System.out.println("业务id:"+processInstance.getBusinessKey());

    }



}

